<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>fetch</title>
    <meta name="description" content="">
    <meta name="keywords" content="">
    <link href="" rel="stylesheet">
    <script type="text/javascript" src="lib/vue.js"></script>

    <!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script> -->
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>

</head>

<body>
    <div id="box">
        <button @click="handleClick()">正在热映</button>
        <button @click="handlePost()">axios发起POST请求</button>

        <ul>
            <li v-for="data in datalist">
                <h3>{{data.name}}</h3>
                <img :src="data.poster" />
            </li>
        </ul>
    </div>

    <!-- http://www.axios-js.com/zh-cn/docs/ -->
    <script>
        new Vue({
            el: "#box",
            data: {
                datalist: []
            },
            methods: {
                handleClick() {
                    axios.get(
                        './json/test.json', {
                        params: {
                            username: "admin",
                            password: "123456"
                        }
                    })
                        .then((response) => {
                            // console.log(response.data);
                            this.datalist = response.data.data.films
                        })
                        .catch((error) => {
                            console.log(error);
                        });
                }
                ,
                handlePost() {
                    /* 默认数据请求格式为JSON */
                    // axios.post('http://www.httpbin.org/post', {
                    //     firstName: 'Fred',
                    //     lastName: 'Flintstone'
                    // })

                    const paramsForm = new URLSearchParams();
                    paramsForm.append('username', 'admin');
                    paramsForm.append('password', '123456');

                    axios.post('http://www.httpbin.org/post', paramsForm)
                        .then(function (response) {
                            console.log(response.data);
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                }
            }
        })
    </script>
</body>

</html>